<?php
class makeRequest{
	protected $url;
	protected $verb;
	protected $rqbody =null;
	protected $length =0;
	protected $rbody =null;
	protected $rinfo =null;

	public function __construct ($url, $verb, $data = null){
		$this->rqbody =$data;
		$this->url = $url;
		$this->verb = $verb;
		if ($this->rbody !== null){
			$this->buildRequest();
		}
	}

	public function buildRequest($data){
		$data = http_build_query($data, '', '&');
		$this->rqbody = $data;
	}

	public function makeRequest (){
		$ch = curl_init($ch);
		try{
			switch (strtoupper($this->verb)){
				case 'GET':
					$tmp = $this->doExecute($ch);
					break;
				case 'DELETE':
					curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); 
					$tmp = $this->doExecute($ch);
					break;
				case 'POST':
					if (!is_string($this->rqbody)){
						$this->buildRequest();
					}
					curl_setopt($ch, CURLOPT_POSTFIELDS, $this->rqbody);
					curl_setopt($ch, CURLOPT_POST, 1);
					$tmp = $this->doExecute($ch);
					break;
				default:
					throw new InvalidArgumentException('Current verb (' . $this->verb . ') is an invalid REST verb.');
			}
		}
		catch (InvalidArgumentException $e){
			curl_close($ch);
			throw $e;
		}
		catch (Exception $e){
			curl_close($ch);
			throw $e;
		}
		return array($tmp,$this->rinfo['http_code']);
	}
	
	protected function doExecute(&$ch){
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		curl_setopt($ch, CURLOPT_URL, $this->url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: application/json'));
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		$this->rbody = curl_exec($ch);
		$this->rinfo	= curl_getinfo($ch);
		curl_close($ch);
			
		return $this->rbody;
	}
}
?>
